$(document).ready(function(){
//	$("#setresumesubmit a.showmore").on("click",function(){
//		var ishide = $("#setresumedetail").is(":hidden");
//		if(ishide){
//			$("#setresumedetail").show();
//		   $(this).closest("#setresumesubmit").addClass("setresumesubmit_open");
//			$(this).text("收起添加更多信息");
//		}
//		else{
//			$("#setresumedetail").hide();
//		   $(this).closest("#setresumesubmit").removeClass("setresumesubmit_open");
//			$(this).text("展开添加更多信息");
//		}
//	});
   $("#uploadresumeform").position({
      my: "left+5 top",
      at: "right top",
      of: $("#upresume_position")
   })
   $('.hr_setresumewrap').on('click', '#selectjobposition,#job_name', function() {
      $(this).jobSelect({
         title: '选择职位',
         listurl: alljoblist_url,
         data:{job_id:$("#job_id").val(),oldjobid:$("#oldjobid").val()},
         selected:function(){
            var fContainer = jQuery(this);
            var target = jQuery(".itemlist a.selectjob.cur_a", fContainer);
            if(target.length > 0){
               jQuery("#job_name").val(target.data('jobtitle'));
               jQuery("#job_id").val(target.data('jobid'));
               //$("#invitejobseeker").show();
            }
            fContainer.modal("hide");
         },
         emptySelected:function(){
            jQuery(".itemlist a.cur_a", $(this)).removeClass("cur_a");
            jQuery("#job_name").val('');
            jQuery("#job_id").val('');           
         },
         cancelSelected:function(){
            jQuery(".itemlist a.cur_a", $(this)).removeClass("cur_a");
            jQuery("#job_name").val('');
            jQuery("#job_id").val('');
            $('.resume_dialogposition').modal('hide');
         }
      });
      
      
   });
	//handle add resume' form validate
   $("#posthandleresume").click(function(){
      $("#resume_handle_form").submit();
   })

   //check user name
   jQuery.validator.addMethod("resumeNameCheck", function(value, element) {
      return element && !(/[`~!@#$%^&*()+={}'\",.\[\]<>\/?]/.test(value));
   }, "姓名不能含有非法字符");

   var validator = {
      rules: {
          name: { resumeNameCheck:true,rangeChars: [1, 20]},
         phone: { mobilephone:true},
         email: { email: true},
           sex: { required: true}
      },
      messages: {
         name:  {
            required: "该项必填",
            rangeChars: "20个以内字符"
         },
         phone: {
            required:  "该项必填",
            mobilephone: "请输入11位手机号"
         },
         email:{
            email : "请输入正确格式的Email"
         }
      },
      submitHandler: function(form) {
         $(form).ajaxSubmit({
            dataType: "json",
            beforeSubmit: function(data, form, options) {
               $('.systeminfo', form).hide().html('');
                  if(!showLoading(form))
                     return false;
               },
               success: function(data) {
                  try {
                     if(data.error) { 
                        $('.systeminfo', form).show().html(data.error);
                     }else if(data.success){
                        $(window).unbind('beforeunload');
                        location.href = data.redirect;	        
                     }
                  } catch(e) {
                     return false;
                  }
              }      
         });
      }         
   };

   $("#resume_handle_form").validate(validator);   
   var deleteUploadAttachment = function(){
      if($.trim($("#fileuploadname").val()).length <= 0)
         return;
      $.ajax({
         async: false,
         type: 'POST',
         url: OC.filePath('resumes', 'ajax', 'deleteattachment.php'),
         dataType: 'json',
         data:{attachname:$("#fileuploadname").val()},
         success: function(result){ 
            $("#fileuploadname,#filerealname,#filename").val('');
         }
     });
   }
   
   jQuery('#uploadresumeform').fileUploadUIX({
      allowDocType:true,
      maxFileSize: 2*1024*1024,
      keepList: false,
      autoUpload: true, 
      onError:function(response){
         $("#uploadresumetable tr:last").remove();
         $(".uploadresumerror").html(response);
         //upload unvalid file,delete existed file
         deleteUploadAttachment();        
         $("#uploadresumeform").position({
            my: "left+5 top",
            at: "right top",
            of: $("#upresume_position")
         })
         return false;
      },
      onCompleteAll: function(list) {
         jQuery.each(list, function(i, o) {
            var handler = o.slice(-1)[0];
            if(handler.response.error) {
               $(".uploadresumerror").html(handler.response.error);                             
            }else{
               $(".uploadresumerror").html('<div class="success">成功上传</div>');
               $("#filerealname").val(handler.response.name);
               $("#fileuploadname,#filename").val(handler.response.upload_name);
               $("#uploadresumetable tr:last").remove();
            }    
         });
         $("#uploadresumeform").position({
            my: "left+5 top",
            at: "right top",
            of: $("#upresume_position")
         })
      }
  });
  //refresh or close page of add new resume,remove
   $(window).bind('beforeunload', function(){
      deleteUploadAttachment();
  });
    
   $("#postresumeback").click(function(){
      window.history.back(-1); 
   });
})
